package mydessertstore.web.servlet;
import mydessertstore.domain.Item;
import mydessertstore.domain.Product;
import mydessertstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ItemShowJsServlet extends HttpServlet
{
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        request.setCharacterEncoding("UTF-8");//设置编码，否则中文显示不出来
        response.setContentType("text/html;charset = UTF-8");//设置编码，否则中文显示不出来

        String itemId = request.getParameter("itemId");
        //itemId得到是http://localhost:8084/Web_DessertStore_war_exploded/itemForm?itemId=EST-15；
        //System.out.println(itemId +"ddddddddd");
        System.out.println("ssssssssssss连接到了servlet");
        itemId=itemId.substring(itemId.length()-6);
        System.out.println(itemId);
        CatalogService catalogService = new CatalogService();

       /* List<Item> productList = catalogService.getItemListByProduct(itemId);
        String resp = "Product ID Name sssss\n";

        int i = 0;
        while(i < productList.size()){
            Item product = productList.get(i);
            resp += product.getProductId() + "      " + product.getItemId() + "\n";
            i++;
        }
        */


       Item item=catalogService.getItem(itemId);//这个itemId怎么动态获取
        Product product= item.getProduct();
       System.out.println("wwwwww");
       String resp =product.getName()+"\n";
       resp+= "类别:"+product.getCategoryId()+"\n"
               +"商品ID:"+item.getItemId()+"\n"
               +"类别ID:"+product.getProductId()+"\n"//productid取不到值
               +" 标价:"+item.getListPrice()+"$"+"\n"
               +"成分:"+item.getAttribute2()+"\n"
               +"口感介绍:"  +item.getAttribute3()+"\n"
               +"重量:"   +item.getAttribute1()+"\n"//中文显示不出来
               +"适合人数:"+item.getAttribute4()+"\n"
               +"餐具:" +item.getAttribute5()+"\n";


        System.out.println("qqqqqqqq");


       // response.setContentType("text/xml");

        PrintWriter out = response.getWriter();
        out.write(resp);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset = UTF-8");

        doPost(request, response);
    }
}